﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace DummyController
{
    public partial class DummyForm : Form
    {
        private JoystickHandler joystick = new JoystickHandler();
        private int servoPos = 2000;

        public DummyForm()
        {
            InitializeComponent();

            string[] ports = SerialPort.GetPortNames();
            comboBoxPorts.Items.AddRange(ports);

            if (comboBoxPorts.Items.Count > 0) comboBoxPorts.SelectedIndex = 0;

            joystick.Init(this.Handle);
        }

        private void buttonOpenCOM_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = (string)comboBoxPorts.SelectedItem;
                serialPort1.Open();
                buttonOpenCOM.Enabled = false;
            }
            catch
            {
            }
        }

        private void timerSender_Tick(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                buttonSend_Click(null, new EventArgs());
            }
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen) return;

            // get data
            float PWM1 = float.Parse(textBoxPWM1.Text);
            float PWM2 = float.Parse(textBoxPWM2.Text);
            float PWM3 = float.Parse(textBoxPWM3.Text);
            float Servo1 = float.Parse(textBoxServo1.Text);
            float Servo2 = float.Parse(textBoxServo2.Text);
            int lights = int.Parse(textBoxLights.Text);
            int sentenceId = 43253453;

            // send data
            string toSend = "$URMAN,";
            toSend += string.Format("{0:0},", sentenceId);
            toSend += string.Format("{0:0},", PWM1);
            toSend += string.Format("{0:0},", PWM2);
            toSend += string.Format("{0:0},", PWM3);
            toSend += string.Format("{0:0},", Servo1);
            toSend += string.Format("{0:0}", Servo2); 
            toSend += "*AA\r";
            serialPort1.Write(toSend);

            toSend = "$URLIG,";
            toSend += string.Format("{0:0},", sentenceId);
            toSend += string.Format("{0:0}", lights);
            toSend += "*BB\r";
            serialPort1.Write(toSend);

            // Read data
            string read = serialPort1.ReadExisting();
            if (read != "")
            {
                string[] splits = read.Split(',');
                if (splits.Length == 5)
                {
                    double consumptionkW = double.Parse(splits[2]) / 1000.0;
                    double BattPercent = double.Parse(splits[3]);

                    textBoxBattPercent.Text = string.Format("{0:0.0} %", BattPercent);
                    textBoxPower.Text = string.Format("{0:0.00} kW", consumptionkW);
                }
                textBoxRXBox.Text = read;
            }
        }

        private void timerTick_Tick(object sender, EventArgs e)
        {
            // read joystick
            JoyState st = joystick.Poll();
            double axisX = st.AxisX;
            double axisY = st.AxisY;
            double thr = st.Throttle;

            // TODO: CALC VALUES
            double pwm1;
            double pwm2;
            double pwm3;
            double srv1;
            double srv2;
            int light = 0;

            pwm1 = thr * 1000 + 1000;
            pwm2 = thr * 1000 + 1000;
            pwm3 = axisY * 500 + 1000;

            pwm1 += axisX * 200;
            pwm2 -= axisX * 200;

            if (st.Button1) light = 1;
            if (st.Button2) servoPos += 10;
            if (st.Button3) servoPos -= 10;

            if (pwm1 < 1000) pwm1 = 1000;
            if (pwm2 < 1000) pwm2 = 1000;
            if (pwm3 < 1000) pwm3 = 1000;
            if (pwm1 > 2000) pwm1 = 2000;
            if (pwm2 > 2000) pwm2 = 2000;
            if (pwm3 > 2000) pwm3 = 2000;

            
            if (servoPos < 0) servoPos = 0;
            if(servoPos > 3500 ) servoPos = 3500;
            srv1 = servoPos;
            srv2 = servoPos;
            

            textBoxPWM1.Text = string.Format("{0:0}", pwm1);
            textBoxPWM2.Text = string.Format("{0:0}", pwm2);
            textBoxPWM3.Text = string.Format("{0:0}", pwm3);

            textBoxServo1.Text = string.Format("{0:0}", srv1);
            textBoxServo2.Text = string.Format("{0:0}", srv2);

            textBoxLights.Text = string.Format("{0}", light); 
        }
    }
}
